package org.afs.gescol.component.edt.gwt.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.afs.gescol.component.edt.gwt.client.bundle.EdtComponentResources;
import org.afs.gescol.component.edt.gwt.client.factory.EdtComponentClientFactory;
import org.afs.gescol.component.edt.gwt.client.presenter.EmploiDuTempsPresenter;
import org.afs.gescol.component.edt.gwt.shared.ConstantesKeysModel;
import org.scub.foundation.framework.gwt.module.client.util.composants.suggestbox.SuggestListBoxSuggestEvent;
import org.scub.foundation.framework.gwt.module.client.util.composants.suggestbox.SuggestListBoxSuggestEventHandler;
import org.scub.foundation.framework.gwt.module.shared.IdLabelModel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.StyleInjector;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * Application entry point.
 * @author Anthony GUILLEMETTE (anthony.guillemette@scub.net)
 */
public final class ClientEntryPointGwt implements EntryPoint {

    /**
     * {@inheritDoc}
     */
    public void onModuleLoad() {
        final ConstantesKeysModel constantesKeys = new ConstantesKeysModel();
        constantesKeys.setDefaultClassKey("DEFAULT_CLASS_KEY");
        constantesKeys.setOpenedPeriodKey("OPENED_PERIOD_KEY");
        constantesKeys.setOpenedYearKey("OPENED_YEAR_KEY");
        final Map<String, IdLabelModel> constantes = new HashMap<String, IdLabelModel>();
        constantes.put(constantesKeys.getOpenedYearKey(), new IdLabelModel(2l, "2012-2013"));
        constantes.put(constantesKeys.getOpenedPeriodKey(), new IdLabelModel(3l, "3ème Trimestre"));
        constantes.put(constantesKeys.getDefaultClassKey(), new IdLabelModel(1l, "6ème"));
        final EdtComponentResources resources = EdtComponentClientFactory.getResources();

        final EmploiDuTempsPresenter edtComponent = EdtComponentClientFactory.getEdtComponent();
        edtComponent.setConstantes(constantes);
        edtComponent.setConstantesKeys(constantesKeys);
        edtComponent.initPresenter();

        StyleInjector.inject(resources.css().getText());

        edtComponent.showPresenter(RootLayoutPanel.get());
        edtComponent.getClassSuggestHandler().addSuggestHandler(new SuggestListBoxSuggestEventHandler<IdLabelModel>() {
            @Override
            public void suggest(SuggestListBoxSuggestEvent<IdLabelModel> event) {
                final List<IdLabelModel> listClasses = new ArrayList<IdLabelModel>();
                listClasses.add(new IdLabelModel(1l, "6ème"));
                listClasses.add(new IdLabelModel(2l, "5ème"));
                event.getCallBack().onSuccess(listClasses);
            }
        });
    }
}